#include <math.h>
#include "2dUtil.h"

void guMtx23RotRad(Mtx23 mt, f32 rad)
{
	f32 sinA,cosA;

	sinA = sinf(rad);
	cosA = cosf(rad);
	mt[0][0] =  cosA;  mt[0][1] = -sinA;  mt[0][2] = 0.0f;
	mt[1][0] =  sinA;  mt[1][1] =  cosA;  mt[1][2] = 0.0f;
}

void guMtx23TransApply(const Mtx23 src, Mtx23 dst,f32 xT,f32 yT)
{
   if ( src != dst )
    {
        dst[0][0] = src[0][0];    dst[0][1] = src[0][1];
        dst[1][0] = src[1][0];    dst[1][1] = src[1][1];
    }
    
    dst[0][2] += xT;
    dst[1][2] += yT;
}

void guMtx23ScaleApply(const Mtx23 src,Mtx23 dst,f32 xS,f32 yS)
{
    dst[0][0] = src[0][0] * xS;     dst[0][1] = src[0][1] * xS;    dst[0][2] = src[0][2] * xS; 
    dst[1][0] = src[1][0] * yS;     dst[1][1] = src[1][1] * yS;    dst[1][2] = src[1][2] * yS;
}

void guMtx24Copy(const Mtx24 src,Mtx24 dst)
{
	if(src==dst) return;

    dst[0][0] = src[0][0];    dst[0][1] = src[0][1];    dst[0][2] = src[0][2];    dst[0][3] = src[0][3];
    dst[1][0] = src[1][0];    dst[1][1] = src[1][1];    dst[1][2] = src[1][2];    dst[1][3] = src[1][3];
}

void guMtx24Concat(const Mtx23 a, const Mtx24 b, Mtx24 ab)
{
	Mtx24 tmp;
	Mtx24P m;

	if(ab==b)
		m = tmp;
	else
		m = ab;

    m[0][0] = a[0][0]*b[0][0] + a[0][1]*b[1][0] + a[0][2];
    m[0][1] = a[0][0]*b[0][1] + a[0][1]*b[1][1] + a[0][2];
    m[0][2] = a[0][0]*b[0][2] + a[0][1]*b[1][2] + a[0][2];
    m[0][3] = a[0][0]*b[0][3] + a[0][1]*b[1][3] + a[0][2];

    m[1][0] = a[1][0]*b[0][0] + a[1][1]*b[1][0] + a[1][2];
    m[1][1] = a[1][0]*b[0][1] + a[1][1]*b[1][1] + a[1][2];
    m[1][2] = a[1][0]*b[0][2] + a[1][1]*b[1][2] + a[1][2];
    m[1][3] = a[1][0]*b[0][3] + a[1][1]*b[1][3] + a[1][2];

	if(m==tmp)
		guMtx24Copy(tmp,ab);
}

// quad with unit length centered about middle.
const Mtx24 unitQuad = {
	{0.0f,  1.0f,  1.0f,  0.0f},
	{0.0f,  0.0f,  1.0f,  1.0f}
};
